package cn.stylefeng.guns.modular.controller;


import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjBanner;
import cn.stylefeng.guns.modular.entity.IdjInformation;
import cn.stylefeng.guns.modular.entity.IdjLists;
import cn.stylefeng.guns.modular.entity.IdjListsUser;
import cn.stylefeng.guns.modular.service.IIdjBannerService;
import cn.stylefeng.guns.modular.service.IIdjInformationService;
import cn.stylefeng.guns.modular.service.IIdjListsService;
import cn.stylefeng.guns.modular.service.IIdjListsUserService;
import cn.stylefeng.guns.modular.util.json.JsonUtils;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 榜单表 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/modular/lists")
public class IdjListsController {

	private static String PREFIX = "/lists/";

	private static final int SHORT_MAX = 65536;

	private static int counter = -1;

	@Autowired
	private IIdjListsService service;
	
	@Autowired
	private IIdjListsUserService userservice;

	/**
	 * 跳转到用户的首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "lists.html";
	}

	@RequestMapping("details/{listsId}")
	public ModelAndView detail(@PathVariable Long listsId) {
		ModelAndView index = service.edit(listsId);
		index.setViewName(PREFIX + "lists_details.html");
		return index;
	}
	
	@RequestMapping("edit/{listsId}")
	public ModelAndView edit(@PathVariable Long listsId) {
		ModelAndView index = service.edit(listsId);
		index.setViewName(PREFIX + "lists_edit.html");
		return index;
	}

	@RequestMapping("/add")
	public String add() {
		return PREFIX + "lists_add.html";
	}

	 /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("list")
    public LayuiPageInfo list(String keyword,String timeLimit, String aa,String bb,String cc, Integer lx) {
        //获取分页参数
        Page<IdjLists> page = LayuiPageFactory.defaultPage();
        //拼接查询条件
        String beginTime = null;
        String endTime = null;
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        String yes="yes";
        //根据条件查询日志
        return service.getList(page, keyword, beginTime, endTime, aa, bb, cc, lx, yes, null);
    }
    
    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("lists")
    public LayuiPageInfo lists(String keyword,String timeLimit, String aa,String bb,String cc, Integer lx) {
    	//获取分页参数
    	Page<IdjLists> page = LayuiPageFactory.defaultPage();
    	//拼接查询条件
    	String beginTime = null;
    	String endTime = null;
    	if (ToolUtil.isNotEmpty(timeLimit)) {
    		String[] split = timeLimit.split(" - ");
    		beginTime = split[0];
    		endTime = split[1];
    	}
    	String no="no";
    	//根据条件查询日志
    	return service.getList(page, keyword, beginTime, endTime, aa, bb, cc, lx, null, no);
    }

	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjLists lists) {
		if (ToolUtil.isEmpty(lists.getListsId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		lists.setUpdateTime(new Date());
		lists.setOperationUserId(LoginContextHolder.getContext().getUserId());
		service.setStatus(lists);
		return ResponseData.success();
	}

	@RequestMapping("/insert")
	@ResponseBody
	public ResponseData insert(@RequestBody IdjLists lists) {
		long id = nextId();
		lists.setListsId(id);
		lists.setCreateTime(new Date());
		lists.setUpdateTime(new Date());
		service.insert(lists);
		JSONArray array = JSONArray.parseArray(lists.getUser());
		for (int i = 0; i < array.size(); i++) {
			IdjListsUser user = new IdjListsUser();
			user.setListUserId(nextId());
			user.setListsId(id);
			user.setUserId(Long.valueOf(array.getString(i)));
			user.setCreateTime(new Date());
			userservice.insert(user);
		}
		return ResponseData.success();
	}

	public static synchronized long nextId() {
		long now = System.currentTimeMillis();
		if (counter == -1) {
			long seed = now ^ Thread.currentThread().getId();
			Random rnd = new Random(Long.hashCode(seed));
			counter = rnd.nextInt(SHORT_MAX);
		}
		long id = (now << 16) | counter;
		counter = (counter + 1) % SHORT_MAX;
		return id;
	}
}

